{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 类和实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Person:\n",
    "    pass\n",
    "\n",
    "zhang3 = Person()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 成员函数和成员变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是张三,性别男,年龄32\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    \n",
    "    def __init__(self):\n",
    "        self.age = None\n",
    "        self.name = None\n",
    "        self.sex = None\n",
    "    \n",
    "    def say(self):\n",
    "        print(f\"我是{self.name},性别{self.sex},年龄{self.age}\")\n",
    "\n",
    "zhang3 = Person()\n",
    "zhang3.name = \"张三\"\n",
    "zhang3.age = 32\n",
    "zhang3.sex = \"男\"\n",
    "\n",
    "zhang3.say()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### \\_\\_new\\_\\_、\\_\\_init\\_\\_ 、 \\_\\_call\\_\\_ 、 \\_\\_len\\_\\_ 、 \\_\\_getitem\\_\\_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是张三,性别男,年龄32\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    \n",
    "    def __init__(self,\n",
    "                 name,\n",
    "                 age,\n",
    "                 sex):\n",
    "        self.age = age\n",
    "        self.name = name\n",
    "        self.sex = sex\n",
    "    \n",
    "    def say(self):\n",
    "        print(f\"我是{self.name},性别{self.sex},年龄{self.age}\")\n",
    "\n",
    "zhang3 = Person(\"张三\",32,\"男\")\n",
    "zhang3.say()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是张三,性别男,年龄32\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "object of type 'Person' has no len()",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[5], line 17\u001b[0m\n\u001b[1;32m     14\u001b[0m zhang3 \u001b[38;5;241m=\u001b[39m Person(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m张三\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;241m32\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m男\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m     15\u001b[0m zhang3()\n\u001b[0;32m---> 17\u001b[0m \u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mzhang3\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: object of type 'Person' has no len()"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    \n",
    "    def __init__(self,\n",
    "                 name,\n",
    "                 age,\n",
    "                 sex):\n",
    "        self.age = age\n",
    "        self.name = name\n",
    "        self.sex = sex\n",
    "    \n",
    "    def __call__(self):\n",
    "        print(f\"我是{self.name},性别{self.sex},年龄{self.age}\")\n",
    "\n",
    "zhang3 = Person(\"张三\",32,\"男\")\n",
    "zhang3()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Person:\n",
    "    \n",
    "    def __init__(self,\n",
    "                 name,\n",
    "                 age,\n",
    "                 sex):\n",
    "        self.age = age\n",
    "        self.name = name\n",
    "        self.sex = sex\n",
    "    \n",
    "    def __call__(self):\n",
    "        print(f\"我是{self.name},性别{self.sex},年龄{self.age}\")\n",
    "    \n",
    "    def __len__(self):\n",
    "        return self.age\n",
    "\n",
    "zhang3 = Person(\"张三\",32,\"男\")\n",
    "len(zhang3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "男\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    \n",
    "    def __init__(self,\n",
    "                 name,\n",
    "                 age,\n",
    "                 sex):\n",
    "        self.age = age\n",
    "        self.name = name\n",
    "        self.sex = sex\n",
    "    \n",
    "    def __call__(self):\n",
    "        print(f\"我是{self.name},性别{self.sex},年龄{self.age}\")\n",
    "    \n",
    "    def __getitem__(self,index):\n",
    "        if index == 0:\n",
    "            return self.name\n",
    "        elif index == 1:\n",
    "            return self.age\n",
    "        elif index == 2:\n",
    "            return self.sex\n",
    "\n",
    "zhang3 = Person(\"张三\",32,\"男\")\n",
    "print(zhang3[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 类函数与类变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我们种族的别名是最智慧的动物\n",
      "我们种族的别名是最智慧的动物\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    alise = \"最智慧的动物\"\n",
    "    \n",
    "    @staticmethod\n",
    "    def say():\n",
    "        print(f\"我们种族的别名是{Person.alise}\")\n",
    "    \n",
    "# Person.alise\n",
    "\n",
    "zhang3 = Person()\n",
    "# zhang3.alise\n",
    "\n",
    "Person.say()\n",
    "zhang3.say()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄是32\n"
     ]
    }
   ],
   "source": [
    "class Animal:\n",
    "    \n",
    "    def __init__(self,age):\n",
    "        self.age = age\n",
    "        \n",
    "class Person(Animal):\n",
    "    \n",
    "    def __init__(self, age):\n",
    "        super().__init__(age)\n",
    "        \n",
    "    def say(self):\n",
    "        print(f\"年龄是{self.age}\")\n",
    "        \n",
    "zhang3 = Person(32)\n",
    "zhang3.say()        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
